package com.gitee.ywj1352.reactive;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

public class SampleSubscription implements Subscription {

    private DecoratingSubscriber subscriber;

    public SampleSubscription(Subscriber subscriber) {
        this.subscriber = new DecoratingSubscriber(subscriber);
    }

    @Override
    public void request(long n) {
        if (n < 1 || n > 5) {
            throw new IllegalArgumentException("The number of elements to requests must be more than zero!");
        }
        System.out.println("SampleSubscription#request" + n);
        subscriber.setMaxRequest(n - 1);
        for (int i = 0; i < n; i++) {
            subscriber.onNext(i);
        }
    }

    @Override
    public void cancel() {
        subscriber.onComplete();
        System.out.println("SampleSubscription#cancel:" + subscriber.getClass());
    }
}
